//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "EuropeanOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/VanillaOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : CVanillaOption(CEuropeanOption::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phEuropeanOption = NULL;
#endif
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppEuropeanOption = new boost::shared_ptr<QuantLib::EuropeanOption> (new QuantLib::EuropeanOption ( _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppEuropeanOption)->setPricingEngine (_QL_Pricer);
        SetVanillaOption (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (boost::shared_ptr<QuantLib::EuropeanOption>& childNative, Object^ owner) : CVanillaOption(CEuropeanOption::typeid)
{
#ifdef HANDLE
	_phEuropeanOption = NULL;
#endif
	_ppEuropeanOption = &childNative;
    _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
}
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (QuantLib::EuropeanOption& childNative, Object^ owner) : CVanillaOption(CEuropeanOption::typeid)
{
#ifdef HANDLE
	_phEuropeanOption = NULL;
#endif
	_ppEuropeanOption = new boost::shared_ptr<QuantLib::EuropeanOption> (&childNative);
    _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
    _EuropeanOptionOwner = owner;
    _VanillaOptionOwner = owner;
}

Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (CEuropeanOption^ copy) : CVanillaOption(CEuropeanOption::typeid)
{
#ifdef HANDLE
	_phEuropeanOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEuropeanOption = new boost::shared_ptr<QuantLib::EuropeanOption> (copy->GetShared());
        _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
    }
}
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (System::Type^ t) : CVanillaOption(CEuropeanOption::typeid)
{
#ifdef HANDLE
	_phEuropeanOption = NULL;
#endif
	if (!t->IsSubclassOf(CEuropeanOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (QuantLib::Handle<QuantLib::EuropeanOption>& childNative, Object^ owner)  : CVanillaOption(CEuropeanOption::typeid)
{
	_phEuropeanOption = &childNative;
	_ppEuropeanOption = &static_cast<boost::shared_ptr<QuantLib::EuropeanOption>>(childNative.currentLink());
    _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
    _EuropeanOptionOwner = owner;
}
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (QuantLib::Handle<QuantLib::EuropeanOption> childNative)  : CVanillaOption(CEuropeanOption::typeid)
{
	_phEuropeanOption = &childNative;
	_ppEuropeanOption = &static_cast<boost::shared_ptr<QuantLib::EuropeanOption>>(childNative.currentLink());
    _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CEuropeanOption::CEuropeanOption (QuantLib::EuropeanOption childNative)  : CVanillaOption(CEuropeanOption::typeid)
{
#ifdef HANDLE
	_phEuropeanOption = NULL;
#endif
	_ppEuropeanOption = new boost::shared_ptr<QuantLib::EuropeanOption> (new QuantLib::EuropeanOption (childNative));
    _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (boost::dynamic_pointer_cast<QuantLib::VanillaOption> (*_ppEuropeanOption));
}
#endif

Cephei::QL::Instruments::CEuropeanOption::~CEuropeanOption ()
{
    if (_ppEuropeanOption != NULL)
    {
	    delete _ppEuropeanOption;
        _ppEuropeanOption = NULL;
    }
}
Cephei::QL::Instruments::CEuropeanOption::!CEuropeanOption ()
{
    if (_ppEuropeanOption != NULL)
    {
	    delete _ppEuropeanOption;
    }
}
QuantLib::EuropeanOption& Cephei::QL::Instruments::CEuropeanOption::GetReference ()
{
    if (_ppEuropeanOption == NULL) throw gcnew NativeNullException ();
	return **_ppEuropeanOption;
}
boost::shared_ptr<QuantLib::EuropeanOption>& Cephei::QL::Instruments::CEuropeanOption::GetShared ()
{
    if (_ppEuropeanOption == NULL) throw gcnew NativeNullException ();
	return *_ppEuropeanOption;
}
QuantLib::EuropeanOption* Cephei::QL::Instruments::CEuropeanOption::GetPointer ()
{
    if (_ppEuropeanOption == NULL) throw gcnew NativeNullException ();
	return &**_ppEuropeanOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EuropeanOption>& Cephei::QL::Instruments::CEuropeanOption::GetHandle ()
{
	if (_phEuropeanOption == NULL)
	{
		_phEuropeanOption = new Handle<QuantLib::EuropeanOption> (*_ppEuropeanOption);
	}
	return *_phEuropeanOption;
}
#endif
bool Cephei::QL::Instruments::CEuropeanOption::HasNative () 
{
	return (_ppEuropeanOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IEuropeanOption^ Cephei::QL::Instruments::CEuropeanOption_Factory::Create (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CEuropeanOption ( payoff,  exercise,  QL_Pricer);
}
